package com.ruoyi.web.web.form;

import com.ruoyi.web.core.config.AppConf;
import com.ruoyi.accountant.domain.Account;
import com.ruoyi.accountant.domain.Currency;
import com.ruoyi.accountant.service.AccountService;
import com.ruoyi.accountant.service.CurrencyService;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.Date;

/***
 * 账户转移表单
 */
@Data
public class TransferForm implements Validatable {
    private Long outaccount; // 转出账户ID
    private Long inaccount; // 转入账户ID
    private Integer outcurrency; // 转出币种
    private Integer incurrency; // 转入币种

    private Double outaccountServiceCharge = 0D; // 转出手续费
    private Double inaccountServiceCharge = 0D; // 转入手续费
    private Date transferDate = new Date(); //转移日期
    private Double transferMoney; //转移金额
    private Integer days; //到账天数
    private String comment; // 备注
    private Long ofCompany;

    private String outaccountName; // 转出账户
    private String inaccountName; // 转入账户
    private String outcurrencyName; // 转出币种
    private String incurrencyName; // 转入币种

    public Validation validate(boolean api, AccountService accountService, CurrencyService currencyService) {
        if(outaccountServiceCharge == null) {
            outaccountServiceCharge = 0D;
        }
        if(inaccountServiceCharge == null) {
            inaccountServiceCharge = 0D;
        }

        if(api) {
            if(StringUtils.isEmpty(outaccountName)) {
                return Validation.error("支出账户名不能为空");
            } else if(StringUtils.isEmpty(inaccountName)) {
                return Validation.error("转入账户名不能为空");
            } else if(StringUtils.isEmpty(outcurrencyName)) {
                return Validation.error("转出币种名不能为空");
            } else if(StringUtils.isEmpty(incurrencyName)) {
               return Validation.error("转入币种名不能为空");
            }

            Account outaccount = accountService.findByAname(outaccountName);
            Account inaccount = accountService.findByAname(inaccountName);

            if(outaccount == null) {
                return Validation.error("支出账户不存在");
            } else if(inaccount == null) {
                return Validation.error("转入账户不存在");
            }

            this.outaccount = outaccount.getAid();
            this.inaccount = inaccount.getAid();


            Currency outCurrency = currencyService.findByEnName(outcurrencyName);
            Currency inCurrency = currencyService.findByEnName(incurrencyName);

            if(outCurrency == null) {
                return Validation.error("不支持此币种："+outcurrencyName);
            } else if(inCurrency == null) {
                return Validation.error("不支持此币种："+inaccountName);
            }

            this.outcurrency = outCurrency.getCid();
            this.incurrency= inCurrency.getCid();
        } else {
            if(this.outaccount == null) {
                return Validation.error("支出账户不能为空");
            } else if(this.inaccount == null) {
                return Validation.error("转入账户不能为空");
            }
            Currency outCurrency = currencyService.findById(this.outcurrency);
            Currency inCurrency = currencyService.findById(this.incurrency);

            if(outCurrency == null) {
                return Validation.error("不支持此币种："+outcurrencyName);
            } else if(inCurrency == null) {
                return Validation.error("不支持此币种："+inaccountName);
            }
        }

        if(outaccountServiceCharge < 0) {
            return Validation.error("转出手续费错误");
        } else if(inaccountServiceCharge < 0) {
            return Validation.error("转入手续费错误");
        }


        // 转移日期不能大于当前日期
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DAY_OF_MONTH, 1);

        if(this.transferDate == null || this.transferDate.compareTo(instance.getTime()) == 1 || this.transferDate.compareTo(instance.getTime()) == 0) {
            return Validation.error("转移日期日期错误");
        }

        if(transferMoney == null || transferMoney <= 0) {
            return Validation.error("转移金额错误");
        }

        if(days == null || days <= 0) {
            return Validation.error("到账天数错误");
        }

        // 检查是否已经停用
        Account byId = accountService.findById(inaccount);
        if(byId.getStop() == AppConf.ON) {
            return Validation.error(byId.getAname()+"已经停用");
        }

        byId = accountService.findById(outaccount);
        if(byId.getStop() == AppConf.ON) {
            return Validation.error(byId.getAname()+"已经停用");
        }

        // 查询币种名称
        this.outcurrencyName = currencyService.findById(this.outcurrency).getEnName();
        this.incurrencyName = currencyService.findById(this.incurrency).getEnName();

        return Validation.ok();
    }
}
